# ———————————————————————–
# <copyright file="SQL.ps1" company="The MITRE Corporation">
#    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#    http:#www.apache.org/licenses/LICENSE-2.0
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# </copyright>
# ———————————————————————–
################################################/
# SQL.ps1 - Script to create IC.NET databases, DB User, and load default data
# Project: IC.NET Installer
# Author: Don McGarry
################################################/

# Copyright (c) 2013 The MITRE Corporation. All rights reserved.
#
# NOTICE
# This software was produced for the U. S. Government
# under Contract No. FA8721-09-C-0001, and is
# subject to the Rights in Noncommercial Computer Software
# and Noncommercial Computer Software Documentation Clause
# (DFARS) 252.227-7014 (JUN 1995)

$Username =$Args[0];
$defaultDataPath= $Args[1];
$defaultLogPath= $Args[2];
$installerRoot= $Args[3];
$hostname=hostname;
$permisions="EXECUTE, DELETE, INSERT, REFERENCES, SELECT, UPDATE";

$AddLoginCmd="USE [master];CREATE LOGIN [$hostname\$Username] FROM WINDOWS WITH DEFAULT_DATABASE=[master];EXEC master..sp_addsrvrolemember @loginame = N'$hostname\$Username', @rolename = N'sysadmin'"
sqlcmd -Q $AddLoginCmd -S "(local)"

$server = New-Object Microsoft.SqlServer.Management.Smo.Server("localhost")

if($server.databases["EDXL"] -eq $NULL)
{
	sqlcmd -i "$installerRoot\SQL\EDXLDB\EDXLDB.publish.sql" -S "(local)" -v DefaultDataPath= `"$defaultDataPath`" DefaultLogPath= `"$defaultLogPath`"
	sqlcmd -Q "USE [EDXL];GRANT $permisions TO [$hostname\$Username]" -S "(local)"
}

if($server.databases["EDXLHistory"] -eq $NULL)
{
	sqlcmd -i "$installerRoot\SQL\EDXLHISTORYDB\EDXLHistoryDB.publish.sql" -S "(local)" -v DefaultDataPath= `"$defaultDataPath`" DefaultLogPath= `"$defaultLogPath`"
	sqlcmd -Q "USE [LOGGING];GRANT $permisions TO [$hostname\$Username]" -S "(local)"
}

if($server.databases["ValueLists"] -eq $NULL)
{
	sqlcmd -i "$installerRoot\SQL\VALUELISTSDB\ValueListDB.publish.sql" -S "(local)" -v DefaultDataPath= `"$defaultDataPath`" DefaultLogPath= `"$defaultLogPath`"
	sqlcmd -Q "USE [ValueLists];GRANT $permisions TO [$hostname\$Username]" -S "(local)"
	#Add Default Data to Tables
	sqlcmd -i "$installerRoot\SQL\SQLDATA\ValueLists\dbo.IC.NETValueList.Table.sql" -S "(local)"
}

if($server.databases["RouterRules"] -eq $NULL)
{
	sqlcmd -i "$installerRoot\SQL\ROUTERRULESDB\RouterRulesDB.publish.sql" -S "(local)" -v DefaultDataPath= `"$defaultDataPath`" DefaultLogPath= `"$defaultLogPath`"
	sqlcmd -Q "USE [RouterRules];GRANT $permisions TO [$hostname\$Username]" -S "(local)"
	#Add Default Data to Tables
	sqlcmd -i "$installerRoot\SQL\SQLDATA\RouterRules\dbo.Rules.Table.sql" -S "(local)"
}


